<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="support.*, java.util.*, java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>


	<%
	Connection conn = null;
	PreparedStatement pStmt = null;
	ResultSet rs = null;
	

    // Registering Postgresql JDBC driver with the DriverManager
    Class.forName("org.postgresql.Driver");

    // Open a connection to the database using DriverManager
    conn = DriverManager.getConnection(
        "jdbc:postgresql://localhost/postgres?" +
        "user=postgres&password=cse135");
    
    conn.setAutoCommit(false);
    
    
    String special = request.getParameter( "specialization" );
    String disc = request.getParameter("major");
    if(special != null){
	    Statement stmt = conn.createStatement();
	    Integer app_id = null;
	    rs = stmt.executeQuery("SELECT app_id from degrees where specialization='"+special + "'");
	    while(rs.next()){
	    	app_id = new Integer(rs.getInt(1));
	 	  	rs = stmt.executeQuery("SELECT * from addresses where app_id="+ app_id.intValue() + "");
	 	  	if(rs.next()){
	 	  		out.println(rs.getString(1) + ",  ");
	 	  		out.println(rs.getString(2) + ",  ");
	 	  		out.println(rs.getString(3) + ",  ");
	 	  		out.println(rs.getString(4) + ",  ");
	 	  		out.println(rs.getString(5) + ",  ");
	 	  		out.println(rs.getString(6) + ",  ");
	 	  		out.println(rs.getString(7) + ",  ");
	 	  		out.println(rs.getString(8) + ",  ");
	 	  		out.println(rs.getString(9) + ",  ");
	 	  		out.println(rs.getString(10) + ",  ");
	 	  		out.println(rs.getString(11) + "  ");
	 	  		out.println("<br>");
	 	  	}
	 	  	rs = stmt.executeQuery("SELECT * from degrees where app_id="+ app_id.intValue() + "");
	 	  	if(rs.next()){
	 	  		out.println(rs.getString(1) + ",  ");
	 	  		out.println(rs.getString(2) + ",  ");
	 	  		out.println(rs.getString(3) + ",  ");
	 	  		out.println(rs.getString(4) + ",  ");
	 	  		out.println(rs.getString(5) + ",  ");
	 	  		out.println(rs.getString(6) + ",  ");
	 	  		out.println(rs.getString(7) + ",  ");
	 	  		out.println(rs.getString(8) + ",  ");
	 	  		out.println(rs.getString(9) + ",  ");
	 	  		out.println(rs.getString(10) + "  ");
	 	  	}
	 	  		
	 	  	out.println("<br>");
	    }
    }
    else if(disc != null){
	    Statement stmt = conn.createStatement();
	    Integer app_id = null;
	    rs = stmt.executeQuery("SELECT app_id from degrees where major='"+disc + "'");
	    while(rs.next()){
	    	out.println("mkcode: " + disc + "<br><br>");
	    	app_id = new Integer(rs.getInt(1));
	 	  	rs = stmt.executeQuery("SELECT * from addresses where app_id="+ app_id.intValue() + "");
	 	  	if(rs.next()){
	 	  		out.println(rs.getString(1) + ",  ");
	 	  		out.println(rs.getString(2) + ",  ");
	 	  		out.println(rs.getString(3) + ",  ");
	 	  		out.println(rs.getString(4) + ",  ");
	 	  		out.println(rs.getString(5) + ",  ");
	 	  		out.println(rs.getString(6) + ",  ");
	 	  		out.println(rs.getString(7) + ",  ");
	 	  		out.println(rs.getString(8) + ",  ");
	 	  		out.println(rs.getString(9) + ",  ");
	 	  		out.println(rs.getString(10) + ",  ");
	 	  		out.println(rs.getString(11) + "  ");
	 	  		out.println("<br>");
	 	  	}
	 	  	rs = stmt.executeQuery("SELECT * from degrees where app_id="+ app_id.intValue() + "");
	 	  	if(rs.next()){
	 	  		out.println(rs.getString(1) + ",  ");
	 	  		out.println(rs.getString(2) + ",  ");
	 	  		out.println(rs.getString(3) + ",  ");
	 	  		out.println(rs.getString(4) + ",  ");
	 	  		out.println(rs.getString(5) + ",  ");
	 	  		out.println(rs.getString(6) + ",  ");
	 	  		out.println(rs.getString(7) + ",  ");
	 	  		out.println(rs.getString(8) + ",  ");
	 	  		out.println(rs.getString(9) + ",  ");
	 	  		out.println(rs.getString(10) + "  ");
	 	  	}
	 	  		
	 	  	out.println("<br>");
	    }
    }
	//// Commit transaction
	conn.commit();
	conn.setAutoCommit(true);
	//pStmt.close();
	conn.close();

%>

</body>
</html>